package com.car.loan.project.tool.controller;

import com.alibaba.fastjson.JSON;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.common.utils.qiniu.QiniuUtil;
import com.car.loan.framework.task.UploadImageTask;
import com.car.loan.project.cd.domain.CdCreditAlbum;
import com.car.loan.project.cd.mapper.CdCreditAlbumMapper;
import com.car.loan.project.cm.domain.CmBankCarType;
import com.car.loan.project.cm.domain.CmBankScheme;
import com.car.loan.project.cm.mapper.CmBankCarTypeMapper;
import com.car.loan.project.cm.mapper.CmBankSchemeMapper;
import com.car.loan.project.ve.domain.VeVehicleAlbum;
import com.car.loan.project.ve.mapper.VeVehicleAlbumMapper;
import com.car.loan.project.xr.domain.Loan;
import com.car.loan.project.xr.domain.LoanAlbum;
import com.car.loan.project.xr.domain.LoanCar;
import com.car.loan.project.xr.domain.LoanDetail;
import com.car.loan.project.xr.domain.LoanInsurance;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.LoanAlbumMapper;
import com.car.loan.project.xr.mapper.LoanCarMapper;
import com.car.loan.project.xr.mapper.LoanDetailMapper;
import com.car.loan.project.xr.mapper.LoanExtMapper;
import com.car.loan.project.xr.mapper.LoanInsuranceMapper;
import com.car.loan.project.xr.mapper.LoanMapper;
import com.car.loan.project.xr.mapper.LoanStatusMapper;
import com.car.loan.project.xr.service.impl.RepaymentInfoService;
import com.car.loan.project.xr.util.enums.LoanBusinessStatus;
import com.qiniu.common.QiniuException;
import io.swagger.annotations.Api;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2020/2/6 14:09
 */
@RestController
@RequestMapping("/tool/data-Handle")
@Api(value = "数据处理", tags = "数据处理")
public class DataHandleController {

    @Resource
    private LoanStatusMapper loanStatusMapper;

    @Resource
    private LoanMapper loanMapper;

    @Resource
    private LoanDetailMapper loanDetailMapper;

    @Resource
    private LoanCarMapper loanCarMapper;

    @Resource
    private LoanExtMapper loanExtMapper;

    @Autowired
    private RepaymentInfoService repaymentInfoService;

    @Resource
    private CmBankCarTypeMapper cmBankCarTypeMapper;

    @Resource
    private CmBankSchemeMapper cmBankSchemeMapper;

    @Resource
    private LoanInsuranceMapper loanInsuranceMapper;

    //老数据导入时用 settleAmount= 实结金额-扣罚金额就行了
    @PostMapping("syncSettleAmount")
    public String syncSettleAmount() {
        List<LoanDetail> loanDetails = loanDetailMapper.selectList(null);
        loanDetails.forEach(loanDetail -> {
            Double salesmanFinedAmount = loanDetail.getSalesmanFinedAmount();
            Double salesmanRebateAmount = loanDetail.getSalesmanRebateAmount();
            loanDetail.setShouldAmount(salesmanRebateAmount - salesmanFinedAmount);
            loanDetailMapper.updateById(loanDetail);
        });
        return "ok";
    }

    @PostMapping("syncCardSpaceRemove")
    public String syncCardSpaceRemove() {
        List<LoanDetail> loanDetails = loanDetailMapper.selectList(null);
        loanDetails.forEach(loanDetail -> {
            if (StringUtils.isNotEmpty(loanDetail.getRepayCardNo())) {
                loanDetail.setRepayCardNo(loanDetail.getRepayCardNo().replace(" ", ""));
            }
            if (StringUtils.isNotEmpty(loanDetail.getDeductionCardNo())) {
                loanDetail.setDeductionCardNo(loanDetail.getDeductionCardNo().replace(" ", ""));
            }
            loanDetailMapper.updateById(loanDetail);
        });
        return "ok";
    }


    @PostMapping("syncLoanInsurance")
    public String syncLoanInsurance() {
        List<LoanCar> loanCars = loanCarMapper.selectList(null);
        loanCars.forEach(loanCar -> {
            Optional<LoanInsurance> loanInsuranceOptional = loanInsuranceMapper
                .selectByLoanId(loanCar.getLoanId());
            loanInsuranceOptional.orElseGet(() -> {
                LoanInsurance loanInsurance = new LoanInsurance();
                BeanUtils.copyBeanProp(loanInsurance, loanCar);
                loanInsurance.setStatus(1);
                loanInsuranceMapper.insert(loanInsurance);
                return loanInsurance;
            });
        });
        return "ok";
    }


    @PostMapping("syncLoanStatus")
    public String syncLoanStatus() {
        List<Loan> loans = loanMapper.selectList(null);
        loans.forEach(loan -> {
            Optional<LoanStatus> optionalLoanStatus =
                loanStatusMapper.selectByLoanId(loan.getId());
            optionalLoanStatus.orElseGet(() -> {
                LoanStatus loanStatus = new LoanStatus();
                loanStatus.setLoanId(loan.getId());
                loanStatus.setBusinessAdminId(loan.getAdminId());
                loanStatus.setBusinessTime(loan.getCreateTime());
                loanStatus.setBusinessStatus((long) LoanBusinessStatus.SUBMIT.status);
                loanStatusMapper.insert(loanStatus);
                return loanStatus;
            });
        });
        return "ok";
    }

    @PostMapping("syncCarInfo")
    public String syncCarInfo() {
        List<Loan> loans = loanMapper.selectList(null);
        loans.forEach(loan -> {
            Long bankSchemeId = loan.getBankSchemeId();
            if (bankSchemeId > 0) {
                CmBankScheme cmBankScheme = cmBankSchemeMapper.selectById(bankSchemeId);
                if (null != cmBankScheme) {
                    CmBankCarType cmBankCarType = cmBankCarTypeMapper
                        .selectById(cmBankScheme.getBankCarTypeId());
                    Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loan.getId());
                    if (optionalLoanCar.isPresent()) {
                        LoanCar loanCar = optionalLoanCar.get();
                        loanCar.setCarType(cmBankCarType.getCarType());
                        loanCar.setCarNature(cmBankScheme.getCarType());
                        loanCarMapper.updateById(loanCar);
                    }
                }
            }
        });

        return "ok";
    }


    private final static String oldQiniuUrl = "http://qiniu.xrqiche.cn/";

    @Resource
    private CdCreditAlbumMapper creditAlbumMapper;

    @Resource
    private VeVehicleAlbumMapper veVehicleAlbumMapper;

    @Resource
    private LoanAlbumMapper loanAlbumMapper;


    @Autowired
    private UploadImageTask uploadImageTask;

    @Autowired
    private QiniuUtil qiniuUtil;

    @PostMapping("syncImg-cd")
    public String syncImgCd() {
        List<CdCreditAlbum> cdCreditAlbums = creditAlbumMapper.selectList(null);
        List<Long> errorList = new ArrayList<>(cdCreditAlbums.size());
        cdCreditAlbums.forEach(cdCreditAlbum -> {
            String url = oldQiniuUrl + cdCreditAlbum.getUrl();
            try {
                System.out.println(cdCreditAlbum.getId());
                //290
                uploadImageTask.uploadImage(url, cdCreditAlbum.getUrl());
            } catch (IOException e) {
                errorList.add(cdCreditAlbum.getId());
                e.printStackTrace();
            }
        });
        return JSON.toJSONString(errorList);
    }

    @PostMapping("syncImg-ve")
    public String syncImgVe() {
        List<VeVehicleAlbum> veVehicleAlbums = veVehicleAlbumMapper.selectList(null);
        List<Long> errorList = new ArrayList<>(veVehicleAlbums.size());
        veVehicleAlbums.forEach(veVehicleAlbum -> {
            String url = oldQiniuUrl + veVehicleAlbum.getUrl();
            try {
                System.out.println(veVehicleAlbum.getId());
                uploadImageTask.uploadImage(url, veVehicleAlbum.getUrl());
            } catch (QiniuException e) {
                errorList.add(veVehicleAlbum.getId());
                e.printStackTrace();
            }
        });
        return JSON.toJSONString(errorList);
    }


    @PostMapping("syncImg-loan")
    public String syncImgLoan() {
        List<LoanAlbum> loanAlbums = loanAlbumMapper.selectList(null);
        List<Long> errorList = new ArrayList<>(loanAlbums.size());
        loanAlbums.forEach(loanAlbum -> {
            String url = oldQiniuUrl + loanAlbum.getUrl();
            System.out.println(loanAlbum.getId());
            try {
                qiniuUtil.putUrl0(url, loanAlbum.getUrl());
            } catch (IOException e) {
                errorList.add(loanAlbum.getId());
                e.printStackTrace();
            }
        });
        return JSON.toJSONString(errorList);
    }

//    @PostMapping("syncRepayInfo")
//    public String syncRepayInfo() {
//        List<Loan> loans = loanMapper.selectList(null);
//        loans.forEach(loan -> {
//
//            Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loan.getId());
//            if(optionalLoanDetail.isPresent()){
//                LoanDetail loanDetail = optionalLoanDetail.get();
//
//                repaymentInfoService.getRepaymentInfo(loan.getId(),loanDetail.getLendDate(),);
//            }
//
//
//        });
//
//        return "ok";
//    }
}
